// =================================================================
// File: Directory.cpp
// Desc: Contains implementations of functions in Directory class
// =================================================================

// =================================================================
// INCLUDES
// =================================================================

#include <map>
#include "Directory.h"

// =================================================================
// FUNCTIONS
// =================================================================

// =================================================================
// Name: Directory::Directory
// Desc: Initializes Directory with inode and parent inode values
// Args: int current_inode - inode of directory
//       int parent_inode - inode of parent directory
// =================================================================
Directory::Directory(int current_inode, int parent_inode)
{
    entries.insert ( pair<string,int>("./", current_inode) );
    entries.insert ( pair<string,int>("../", parent_inode) );
    this->SetInodeIndex(current_inode);
}

// =================================================================
// Name: Directory::Directory
// Desc: Initializes Directory with its inode value
// Args: int current_inode - inode of directory
// =================================================================
Directory::Directory(int current_inode)
{
    this->SetInodeIndex(current_inode);
}

// =================================================================
// Name: Directory::~Directory
// Desc: Directory destructor
// =================================================================
Directory::~Directory()
{
}

// =================================================================
// Name: Directory::GetEntry
// Desc: Retrieves an inode given a filename
// Args: string filename - filename in directory
// Rets: corresponding inode of filename
// =================================================================
int Directory::GetEntry(string filename) {
    map<string, int>::iterator it;
    it = entries.find(filename);

    if (it != entries.end())
        return it->second;
    else
        return -1;
}

// =================================================================
// Name: Directory::InsertEntry
// Desc: Inserts a filename, inode pair into the map
// Args: string filename - filename to insert
//       int inode - corresponding inode to insert
// =================================================================
void Directory::InsertEntry(string filename, int inode) {
    
    entries.insert(pair<string, int>(filename, inode));
}

// =================================================================
// Name: Directory::RemoveEntry
// Desc: Removes an entry from the map given the filename
// Args: string filename - filename to remove from the map
// =================================================================
void Directory::RemoveEntry(string filename) {

    map<string,int>::iterator it;
    it = entries.find(filename);

    if (it != entries.end())
        entries.erase(it);
}

// =================================================================
// Name: Directory::GetEntries
// Desc: Accessor function to map
// Rets: entire map in Directory
// =================================================================
map<string, int> Directory::GetEntries() {
    return entries;
}

